package com.whu.waterworks.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;

import com.sun.org.apache.bcel.internal.generic.Select;
import com.whu.waterworks.dao.WaterPlantDao;
import com.whu.waterworks.model.WaterPlant;

public class WaterPlantDaoImpl implements WaterPlantDao {
	HibernateTemplate hibernateTemplate ;
	@Override
	public List<WaterPlant> getAll() {
		@SuppressWarnings("unchecked")
		List<WaterPlant> waterPlants =(List<WaterPlant>) hibernateTemplate.find("from WaterPlant _WaterPlant order by _WaterPlant.waterPlantCode desc");		
		if (waterPlants==null) {
			return null;
		}
		else {
			System.out.println("WaterPlantDaoImpl waterPlants="+waterPlants.get(0).getWaterPlantName());
			return waterPlants;
		}
	}

	@Override
	public WaterPlant getWaterPlantById(String waterPlantCode) {
		List<WaterPlant> waterPlants =(List<WaterPlant>) hibernateTemplate.find("from WaterPlant _WaterPlant where _WaterPlant.waterPlantCode='"+waterPlantCode+"'");
		if (waterPlants==null) {
			return null;
		}
		else {
			System.out.println("WaterPlantDaoImpl waterPlants="+waterPlants.get(0).getWaterPlantName());
			return waterPlants.get(0);
		}
	}

	@Override
	public WaterPlant getWaterPlantByName(String waterPlantName) {
		@SuppressWarnings("unchecked")
		List<WaterPlant> waterPlants =(List<WaterPlant>) hibernateTemplate.find("from WaterPlant _WaterPlant where _WaterPlant.waterPlantName='"+waterPlantName+"'");
		if (waterPlants==null) {
			return null;
		}
		else {
			return waterPlants.get(0);
		}
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	/* (non-Javadoc)
	 * @see com.whu.waterworks.dao.WaterPlantDao#saveWaterPlant(com.whu.waterworks.model.WaterPlant)
	 */
	@Override
	public boolean saveWaterPlant(WaterPlant waterPlant) {
		// TODO Auto-generated method stub
		hibernateTemplate.save(waterPlant);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.whu.waterworks.dao.WaterPlantDao#updateWaterPlant(com.whu.waterworks.model.WaterPlant)
	 */
	@Override
	public boolean updateWaterPlant(WaterPlant waterPlant) {
		// TODO Auto-generated method stub
		hibernateTemplate.merge(waterPlant);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.whu.waterworks.dao.WaterPlantDao#delWaterPlant(com.whu.waterworks.model.WaterPlant)
	 */
	@Override
	public boolean delWaterPlant(WaterPlant waterPlant) {
		// TODO Auto-generated method stub
		hibernateTemplate.delete(waterPlant);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.whu.waterworks.dao.WaterPlantDao#getWaterPlantCode()
	 */
	@Override
	public WaterPlant getWaterPlantOfMaxCode() {
		// TODO Auto-generated method stub
		hibernateTemplate.setMaxResults(1);
		@SuppressWarnings("unchecked")
		ArrayList<WaterPlant> rs =(ArrayList<WaterPlant>) hibernateTemplate.find("from QualityNormol _QualityNormol where _QualityNormol.type='1'");
		hibernateTemplate.setMaxResults(0);
		if(null!=rs && rs.size()>0){
			return rs.get(0);
		}
		return null;
	}

	@Override
	public int getBackwashPumpNum(String waterPlantCode) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Object> bckwashPumpNum = (ArrayList<Object>)hibernateTemplate.find("Select w.backwashPumpNum from WaterPlant w where w.waterPlantCode='"+waterPlantCode+"'");
		if(bckwashPumpNum!=null && bckwashPumpNum.size()>0)
		{
			//System.out.println("WaterPlantDaoImpl^^^^^^"+(int)bckwashPumpNum.get(0));
			return (int)bckwashPumpNum.get(0);
		}
		else {
			return 0;
		}
		
	}

	@Override
	public int getBackwashFanNum(String waterPlantCode) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Object> bckwashPumpNum = (ArrayList<Object>)hibernateTemplate.find("Select w.backwashFanNum from WaterPlant w where w.waterPlantCode='"+waterPlantCode+"'");
		if(bckwashPumpNum!=null && bckwashPumpNum.size()>0)
		{
			return (int)bckwashPumpNum.get(0);
		}
		else {
			return 0;
		}
	}

	@Override
	public int getSupplyPumpNum(String waterPlantCode) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Object> bckwashPumpNum = (ArrayList<Object>)hibernateTemplate.find("Select w.supplyPumpNum from WaterPlant w where w.waterPlantCode='"+waterPlantCode+"'");
		if(bckwashPumpNum!=null && bckwashPumpNum.size()>0)
		{
			return (int)bckwashPumpNum.get(0);
		}
		else {
			return 0;
		}
	}

	@Override
	public int getGetPumpNum(String waterPlantCode) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Object> bckwashPumpNum = (ArrayList<Object>)hibernateTemplate.find("Select w.getPumpNum from WaterPlant w where w.waterPlantCode='"+waterPlantCode+"'");
		if(bckwashPumpNum!=null && bckwashPumpNum.size()>0)
		{
			return (int)bckwashPumpNum.get(0);
		}
		else {
			return 0;
		}
	}

	
	

}
